package com.cn.adapters;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import cc.util.android.view.CCInnerListView;

import com.cn.adapters.ImageAndTextListAdapter.ImageListenr;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.simu.towercrane.R;

public class BusinessmanAdapter extends BaseAdapter{
	private String IMG_FILD = "ImageURL";
	private int mResID;
	private List<Map<String, Object>> mList;
	private ListView mListView;
	private ArrayList<String> listImage;
	private ImageLoader mImageLoader;
	private ViewGroup group ;
	private LayoutInflater mInflater;
	private String[] mFrom;
	private int[] mTo;
	private int count = 0;
	private Context mContext;
	private View viewcontext;
	private ImageListenr mImageListener;
	
	/*
	 * private int[] mInvokedIDs; private OnClickListener[] mListeners;
	 */

	private int mBackgroundResID = -1;
	private LinkedHashMap<String, SoftReference<Bitmap>> lruCache;

	public BusinessmanAdapter (Context context,
			List<Map<String, Object>> list, int resourceid, String[] from,
			int[] to) {
	
		mContext=context;
		this.mList = list;
		this.mResID = resourceid;
		this.mFrom = from;
		this.mTo = to;
		mImageLoader = ImageLoader.getInstance();
		this.mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		lruCache = new LinkedHashMap<String, SoftReference<Bitmap>>(20, 0.75f, true) {
			private static final long serialVersionUID = -2920787383821401845L;

			@Override
			protected boolean removeEldestEntry(
					java.util.Map.Entry<String, SoftReference<Bitmap>> eldest) {
				boolean remove = size() > 20;
				if (remove) {
					SoftReference<Bitmap> soft = eldest.getValue();
					soft.clear();
				}
				return remove;
			}
		};
	}

	public void setListView(ListView listView) {
		this.mListView = listView;
	}

	public void setItemBackgroud(int resID) {
		this.mBackgroundResID = resID;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Deprecated
	public void setImgFild(String fild) {
		this.IMG_FILD = fild;
	}

/*	public void notifyCancelLoadImage() {
		mImageLoader.cancel(true);
	}*/

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {		
		View v = null;
		
		if (mListView instanceof CCInnerListView) {
			v = mInflater.inflate(mResID, parent, false);
		} else {
			if (convertView == null)
			{
				v = mInflater.inflate(mResID, parent, false);
				viewcontext=v;
			}
			else 
			{
				v = convertView;
			viewcontext=v;
			}
		}
		
		bindView(mList.get(position), v);
		if (mBackgroundResID != -1)
			v.setBackgroundResource(mBackgroundResID);
		return v;
	}

	public void invokeListener(int[] ids, OnClickListener[] listeners) {
		/*
		 * this.mInvokedIDs = ids; this.mListeners = listeners;
		 */
	}

	@SuppressWarnings("unchecked")
	private void bindView(Map<String, Object> map, View v) {
		//listImage=(ArrayList<String>)map.get("url");
		final int length = mFrom.length;
//		if(listImage!=null)
//		{
//			ViewGroup group = (ViewGroup) v.findViewById(R.id.viewGroup);
//			ImageView[] imageViews = new ImageView[listImage.size()];
//			for (int i = 0; i < imageViews.length; i++) {
//				final ImageView imageView = new ImageView(mContext);
//				imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,200));
//				imageView.setPadding(20, 0, 0, 0);
//				
//				imageViews[i] = imageView;
//				ImageLoader.getInstance().displayImage(listImage.get(i)+"", imageViews[i]);
//				imageViews[i].setOnClickListener(new OnClickListener() {
//					
//					@Override
//					public void onClick(View arg0) {
//						// TODO Auto-generated method stub
//						if(mImageListener!=null){
//							mImageListener.responeImageListner(imageView);
//						}
//					}
//				});
//				group.addView(imageView);
//				
//			}

			
			
	//	}
		
		
		for (int i = 0; i < length; i++) {
			final View view = v.findViewById(mTo[i]);
			if (view != null) {
				//根据返回的数据的编码方式不同而不同
				final Object data = map.get(mFrom[i]);
				String dataDe=data+"";
//				byte[] bytes=Base64.decode(dataEn.getBytes(), Base64.DEFAULT);
//				String dataDe="";
//				try {
//					 dataDe=new String(bytes,"gb2312");
//				} catch (UnsupportedEncodingException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
				if (view instanceof TextView) {					

					//((TextView)view).setText(data == null ? "" : data + "");
					((TextView)view).setText(dataDe);
				} else if (view instanceof ImageView) {
					if(!dataDe.equals("")){
						Log.d("img", dataDe);
					}
					//dataDe="http://www.cncfzx.com"+dataDe;
					ImageView imageView = (ImageView)view;
					imageView.setTag(dataDe + "");
					SoftReference<Bitmap> soft = lruCache.get(dataDe + "");
					Bitmap bitmap = soft == null ? null : soft.get();
					if (bitmap != null) {
						//imageView.setImageBitmap(bitmap);
						BitmapDrawable bt=new BitmapDrawable(bitmap);
						imageView.setBackgroundDrawable(bt);
					} else {
						//imageView.setImageResource(R.drawable.def_img);
						imageView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.ic_stub));
						//mImageLoader.loadImage(dataDe + "", imageView);
						mImageLoader.displayImage(dataDe + "", imageView);
					}
				} else {
					Log.e("error",v.getClass().getName() + " is not a "
							+ " view that can be bounds by this SimpleAdapter");
				}
			}
		}
		/*
		 * for (int i = 0; i < mInvokedIDs.length; i++) { View view =
		 * v.findViewById(mInvokedIDs[i]);
		 * view.setOnClickListener(mListeners[i]); }
		 */
	}	
//	public void setImageListener(ImageListenr iamgeListner){
//		this.mImageListener=iamgeListner;
//	}
//
//	public interface ImageListenr{
//		public void responeImageListner(ImageView imageView);
//	}
}
